package Control;

import Model.Message;
import Model.login;
import Model.student;
import Service.Crudservice;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/crud")
public class CrudControl extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");
        String type = servletRequest.getParameter("type");


        Crudservice crudservice = new Crudservice();

        try {
            switch (type) {
                case "login": {
                    String name = servletRequest.getParameter("name");
                    String password = servletRequest.getParameter("pwd");
                    Boolean login1 = true;
                    Boolean login = crudservice.login(name, password);
//                    System.out.println("login = " + login);
                    PrintWriter writer = servletResponse.getWriter();
                    if (login == null) {
                        login1 = false;
                    }
                    if (login1) {
                        HttpServletRequest servletRequest1 = (HttpServletRequest) servletRequest;
                        HttpSession session = servletRequest1.getSession();
                        session.setAttribute("YN", "666");
                        String yz = servletRequest.getParameter("yz");
                        if (yz.equals("")) {
                            writer.print("emptycode");
                            return;
                        }
                        if (!(yz.equals(session.getAttribute("yz")))) {
                            writer.print("error");
                            return;
                        }
                    } else {
                        writer.print("nologin");
                        System.out.println("no");
                    }
                }
                break;
                case "serach": {
                    int id = Integer.parseInt(servletRequest.getParameter("id"));
                    PrintWriter writer = servletResponse.getWriter();
                    ArrayList<student> serach = crudservice.serach(id);
                    if (serach.size() == 0) {
                        writer.print("without");
                    } else {
                        String s = JSON.toJSONString(serach);
                        writer.print(s);
                        writer.flush();
                        writer.close();
                    }

                }
                break;
                case "del": {
                    String id = servletRequest.getParameter("id");
                    Integer id1 = Integer.valueOf(id);
                    crudservice.delete(id1);
                    PrintWriter writer = servletResponse.getWriter();
                    Message del = new Message("200", "删除成功");
                    writer.print(JSONObject.toJSON(del));
                }
                break;
                case "delmore": {
                    String[] studentIds = servletRequest.getParameterValues("studentIds");
                    int[] ints = new int[studentIds.length];
                    for (int i = 0; i < ints.length; i++) {
                        ints[i] = Integer.parseInt(studentIds[i]);
                        System.out.println(ints[i]);
                    }
                    crudservice.delmore(ints);
                    PrintWriter writer = servletResponse.getWriter();
                    Message delmore = new Message("200", "批量删除成功");
                    writer.print(JSONObject.toJSON(delmore));
                }

                break;
                case "add": {
                    String name = servletRequest.getParameter("name");
                    Boolean same = crudservice.same(name);
                    if (same == null) {
                        same = false;
                    }
                    if (!same) {
                        student studentnew = new student();
                        studentnew.setName(servletRequest.getParameter("name"));
                        studentnew.setAge(Integer.valueOf(servletRequest.getParameter("age")));
                        studentnew.setHeight(servletRequest.getParameter("height"));
                        studentnew.setSchool(servletRequest.getParameter("school"));
                        crudservice.insert(studentnew);
                        PrintWriter writer = servletResponse.getWriter();
                        Message ins = new Message("200", "添加成功");
                        writer.print(JSONObject.toJSON(ins));
                    } else {
                        servletRequest.getRequestDispatcher("same.jsp").forward(servletRequest, servletResponse);
                    }
                }
                break;
                case "update": {
                    student studentnew = new student();
                    studentnew.setId(Integer.parseInt(servletRequest.getParameter("id")));
                    studentnew.setName(servletRequest.getParameter("name"));
                    studentnew.setAge(Integer.valueOf(servletRequest.getParameter("age")));
                    studentnew.setHeight(servletRequest.getParameter("height"));
                    studentnew.setSchool(servletRequest.getParameter("school"));
                    crudservice.update(studentnew);
                    PrintWriter writer = servletResponse.getWriter();
                    Message del = new Message("200", "修改成功");
                    writer.print(JSONObject.toJSON(del));
                }
                break;
                case "find": {
                    String ansewer1 = servletRequest.getParameter("ansewer1");
                    String ansewer2 = servletRequest.getParameter("ansewer2");
                    String user = servletRequest.getParameter("user");
                    PrintWriter writer = servletResponse.getWriter();
                    if (ansewer1.equals("爱") && ansewer2.equals("HY")) {
                        login login = crudservice.selectOnelogin(user);
                        writer.print(login);
                        System.out.println(login);
                    }
                }
                break;
                //防跳墙
                case "stop": {
                    HttpServletRequest servletRequest1 = (HttpServletRequest) servletRequest;
                    HttpSession session = servletRequest1.getSession();
                    PrintWriter writer = servletResponse.getWriter();
                    String yn = (String) session.getAttribute("YN");
                    System.out.println(yn);
                    writer.print(yn);
                }

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
